package com.ruoyi.jksd.common;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.jksd.common.dto.XySdDto;
import com.ruoyi.jksd.domain.XyBusinessConfig;
import com.ruoyi.jksd.domain.XyConvey;
import com.ruoyi.jksd.domain.XyGoodsList;
import com.ruoyi.jksd.domain.XyUsers;
import com.ruoyi.jksd.service.IXyBusinessConfigService;
import com.ruoyi.jksd.service.IXyConveyService;
import com.ruoyi.jksd.service.IXyGoodsListService;
import com.ruoyi.jksd.service.IXyUsersService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 下单组件
 */
@Component
public class SdComponent {

    @Autowired
    IXyGoodsListService xyGoodsListService;
    @Autowired
    IXyUsersService xyUsersService;
    @Autowired
    IXyConveyService xyConveyService;

    @Autowired
    IXyBusinessConfigService xyBusinessConfigService;

    public XySdDto checkSelectedGoodsList(XyUsers xyUsers) {
        //查询没有完成订单
        List<XyConvey> xyConveyList = xyConveyService.selectUnPayConvey(xyUsers.getId());
        if (CollectionUtils.isNotEmpty(xyConveyList)) {
            XyConvey xyConvey = xyConveyList.get(0);
//            xyConvey.getGoodsId();
            XyGoodsList xyGoodsList = xyGoodsListService.selectXyGoodsListById(xyConvey.getGoodsId());
            XySdDto xySdDto = new XySdDto();
            xySdDto.setXyGoodsList(xyGoodsList);
            xySdDto.setXyConvey(xyConvey);
            xySdDto.setAmount(xyConvey.getNum());
            xySdDto.setCount(xyConvey.getGoodsCount());
            xySdDto.setPrice(xyConvey.getGoodsPrice());
            return xySdDto;
        }

        //如果用户所有订单已经完成，则随机产生新得订单

        /**
         * 此处根据用户余额自动匹配对应得商品,匹配逻辑在余额得80%
         */
        XySdDto xySdDto = rand_order_bate(xyUsers);
        //标记用得刷单行为和数量
        BigDecimal commission = new BigDecimal(0);
        if (1 == xySdDto.getIskd()) {
            commission = xySdDto.getAmount().multiply(new BigDecimal(xyUsers.getDzy())).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_DOWN);
        } else {
            List<XyBusinessConfig> businessConfigs = xyBusinessConfigService.selectXyBusinessConfigList(null);
            XyBusinessConfig xyBusinessConfig = businessConfigs.get(0);
            commission = xySdDto.getAmount().multiply(new BigDecimal(xyBusinessConfig.getTradeCommissionRate()).divide(new BigDecimal(100)));

        }

        XyConvey newXyconvey = new XyConvey();
        newXyconvey.setId(JksdUtils.buildSn("UB"));
        newXyconvey.setUid(Long.valueOf(xyUsers.getId()));
        newXyconvey.setGoodsId(xySdDto.getXyGoodsList().getId());
        newXyconvey.setGoodsCount(xySdDto.getCount());
        newXyconvey.setNum(xySdDto.getAmount());
        newXyconvey.setGoodsPrice(xySdDto.getXyGoodsList().getGoodsPrice());
        newXyconvey.setAddtime(System.currentTimeMillis() / 1000);
//        newXyconvey.setEndtime(System.currentTimeMillis() + 24 * 3600 * 1000);
        newXyconvey.setCommission(commission);
        newXyconvey.setStatus(0L);
        xyConveyService.insertXyConvey(newXyconvey);
        //更新用户信息
        if (1 == xySdDto.getIskd()) {
            xyUsers.setDealStatus(3);
            xyUsers.setDealCount(xyUsers.getDealCount() + 1);
            xyUsers.setDealTime(System.currentTimeMillis());
            xyUsers.setDz(0l);
            xyUsers.setDzc(0L);
            xyUsers.setDzy(0L);
            xyUsersService.updateXyUsers(xyUsers);
        }

        return xySdDto;

    }


    private XySdDto rand_order_bate(XyUsers xyUsers) {
        BigDecimal bigDecimal = xyUsers.getBalance();
        XySdDto xySdDto = new XySdDto();
        //判断是否对客户打针
        if (xyUsers.getDzc() > 0 && xyUsers.getDzc().longValue() == xyUsers.getDealCount().longValue() + 1) {
            Long dzMoney = xyUsers.getDz();
            XyGoodsList xyGoodsList = new XyGoodsList();
            xyGoodsList.setGoodsPrice(new BigDecimal(dzMoney));
            List<XyGoodsList> tmepGoodsList = xyGoodsListService.selectXyGoodsListList(xyGoodsList);
            if (CollectionUtils.isNotEmpty(tmepGoodsList)) {
                xyGoodsList = tmepGoodsList.get(0);
                xySdDto.setXyGoodsList(xyGoodsList);
                xySdDto.setCount(1L);
                xySdDto.setAmount(xyGoodsList.getGoodsPrice());
                xySdDto.setIskd(1);
                return xySdDto;
            } else {
                //如果没有金额相似得
                XyGoodsList tmpXygoods = xyGoodsListService.selectXyGoodsListForDz(dzMoney);
                xySdDto.setXyGoodsList(tmpXygoods);
                xySdDto.setCount(1L);
                xySdDto.setAmount(tmpXygoods.getGoodsPrice());
                xySdDto.setIskd(1);
                return xySdDto;
            }
        } else {
            Long min = xyUsers.getBalance().longValue() * 80 / 100;
            BigDecimal minMoney = new BigDecimal(min);
            BigDecimal nowMoney = xyUsers.getBalance();
            XyGoodsList nowGoods;
            XyGoodsList tmpXygoodsList = xyGoodsListService.selectXyGoodsListForSd(minMoney, nowMoney);
            if (null != tmpXygoodsList) {
                nowGoods = tmpXygoodsList;
            } else {
                XyGoodsList randXygoodsList = xyGoodsListService.selectXyGoodsListForSdrand(minMoney, nowMoney);
                nowGoods = randXygoodsList;
            }

            long count = nowMoney.divide(nowGoods.getGoodsPrice(), 0, RoundingMode.HALF_UP).longValue();
            count = count < 1 ? 1 : count;
            BigDecimal amount = nowGoods.getGoodsPrice().multiply(new BigDecimal(count));
            if (amount.compareTo(xyUsers.getBalance()) > 0) {
                count = count - 1;
            }
            xySdDto.setXyGoodsList(nowGoods);
            xySdDto.setPrice(nowGoods.getGoodsPrice());
            xySdDto.setAmount(nowGoods.getGoodsPrice().multiply(new BigDecimal(count)));
            xySdDto.setCount(count);
        }
        return xySdDto;
    }


}
